// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var constant = require("constant");
	var css_detail = require("./adminiBusinessAccount.css");
	var databind = require("databind");
	var html_bankInfo_create = require("./html_bankInfo_create2.html");
	var pageIndex = 1, pageSize = 10, totalPageCount = 1, totalRecordCount = 0; // 分页初始化
	var tools = require("tools");
	var dialog = require("dialog");
	var creditCard;
	var accountHolder;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function() {
		router.init();
		showList();
		initEvent();
	};

	var initEvent = function() {
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		}

		$("#applyType span").click(function() {
					$("#applyType span").removeClass("active_applyType");
					$(this).addClass("active_applyType");
					$(this).parent().removeClass("applyType1").removeClass("applyType2").removeClass("applyType3").removeClass("applyType4").removeClass("applyType5").removeClass("applyType6");
					$(this).parent().addClass($(this).attr("applytype"));
					$("#accountRecordApplySearchButton").attr("action",$(this).attr("action"));
					showList($(this).attr("action"));
				});

		$("#adminiBusinessAccount .table-search-button").unbind("click").click(function() {
			pageIndex = 1;
			showList();
		});
		$("#adminiBusinessAccount .createBlankInfo").unbind("click").click(function(){
			$("#popdialog").html(html_bankInfo_create);
			dialog.showDialog($("#create_bankInfo_dialog"));
			$("#create_bankInfo_dialog .dialogHandlerBtn").unbind("click").click(function(){
				databind.submitDom($("#create_bankInfo_dialog"), function(){
					base.alert(1, "创建成功！");
					dialog.hideDialog($("#create_bankInfo_dialog"));
					router.route("account/adminiBusinessAccount.html");
				});
			});
			
			$(".dialogContent").on("change", "#creditCard", function(){
				if ($.trim($(this).val()) == "") {
					return;
				}
				var bn = tools.getBankName($(this).val());
				if (bn == ""){
					base.alert(2, "未知开户行！");
					$("#accountHolder").removeAttr("disabled");
					$("#accountHolder").removeAttr("readonly");
				} else {
					$("#accountHolder").val(bn);
					$("#accountHolder").attr("disabled", "disabled");
					$("#accountHolder").attr("readonly", "readonly");
				}
			});
		});
	};
	
    //搜索按钮
    $(".table-search-button").on("click",function(){
        pageIndex = 1;
		pageSize = 10;
		totalPageCount = 1;
		totalRecordCount = 0; //分页初始化
		showList();
    });

	// 申请列表
	var showList = function() {
//		var body = base.parseForm($("#adminiBusinessAccount"));
		var accountHolder=$(".accountHolder").val();
		var defaultAccount=$("#adminiBusinessAccount select[name=defaultAccount]").val();
		base.fnAjax({
					action : "queryAccountManagesPageList.json",
					async : false,
					oData : {
						pageIndex : pageIndex,
						pageSize : pageSize,
						accountHolder : accountHolder,
						depositBank : $("#adminiBusinessAccount .depositBank").val(),
						subbranch : $("#adminiBusinessAccount .subbranch").val(),
						creditCard : $("#adminiBusinessAccount .creditCard").val(),
						defaultAccount : defaultAccount
					}, // 传递参数
					fnSuccess : function(data) {
						if (data.status == 200) {
							if (data.status == 200) {
								juicer.register("getValueByKey",constant.getValueByKey);
//								juicer.register("formatCurrency",tools.formatCurrency);
								var tpl = $("#enterpriseListTpl").html();
								var html = juicer(tpl, data);
								$("#enterpriseListBody").html(html);
								totalPageCount = data.totalPageCount;
								totalRecordCount = data.totalRecordCount;
								base.pageControl($("#clientPagenation"),totalRecordCount, data.pageIndex,data.pageSize, function(p) {
										pageIndex = p;
										showList();
								});
								// 点击详情
								$("#enterpriseListBody").on("click",".table_details_link",
									function() {
										pageIndex = 1;
										var accountIds = [];
										var $this = $(this).parents("tr.tr_style");
										accountIds.push($this.attr("accountId"));
										router.route("account/businessAccount_details.html?accountId="+ accountIds,true);
								});

								// 置顶
//								$("#enterpriseListBody .top").on("click",".top",function() {
								$("#enterpriseListBody").on("click",".top",function() {
										var accountIds = [];
										var $this = $(this).parents("tr.tr_style");
										accountIds.push($this.attr("accountId"));
										topBlankInfo(accountIds);
//										router.route("account/adminiBusinessAccount.html");
									});

								// 取消置顶
								$("#enterpriseListBody .cancelTop").unbind("click").click(function() {
									var accountIds = [];
									var $this = $(this).parents("tr.tr_style");
									accountIds.push($this.attr("accountId"));
									cancelTopBlankInfo(accountIds);
//									router.route("account/adminiBusinessAccount.html");
								});

								tools.inquireCurrentOperationList($("#main"));

							} else {
								base.alert(2, "信息获取失败");
							}
						}
					}
				});
	};
	// 置顶
	var topBlankInfo = function(accountInds) {
		base.fnAjax({
			action : "topAccountInfo.json", // 接口名称
			oData : {
				accountId : accountInds
			}, // 传递参数
			fnSuccess : function(data) { // 成功回调
				if (data.status == 200) {
					base.alert(1, "置顶成功！");
					window.location.reload();
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};

	// 取消置顶
	var cancelTopBlankInfo = function(accountId) {
		base.fnAjax({
			action : "cancelTopAccountInfo.json", // 接口名称
			oData : {
				accountId : accountId
			}, // 传递参数
			fnSuccess : function(data) { // 成功回调
				if (data.status == 200) {
					base.alert(1, "取消置顶成功！");
					window.location.reload();
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};
	return {
		init : init
	};
});